$(function () {
	showOnOff();
	showUser();
	getCarousel()

	// 自定义页面跳转
	$(".self").click(function () {
		window.location.href = "./self.html";
	});

	// 登出功能
	$(".logout").click(function () {
		const userId = sessionStorage.getItem("id");
		const token = sessionStorage.getItem("token");
		axios
			.get(`http://localhost:9000/users/logout?id=${userId}`, {
				headers: {
					Authorization: token,
				},
			})
			.then(logoutResponse => {
				if (logoutResponse.data.code === 1) {
					// 清空sessionStorage中的user数据
					sessionStorage.clear();
					window.location.href = "./index.html";
				}
			});
	});
});

function getCarousel() {
	axios.get("http://localhost:9000/carousel/list").then(response => {
		const images = response.data.list
			.map(
				item => `<div><img src="http://localhost:9000/${item.name}" /></div>`
			)
			.join("");
		const carouselHtml = `<div carousel-item>${images}</div>`;

		$("#carousel").html(carouselHtml);

		layui.carousel.render({
			elem: "#carousel",
			width: "1200px",
			height: "600px",
			arrow: "hover",
			anim: "fade",
		});
	});
}

function showUser() {
	const nickname = sessionStorage.getItem("nickname");
	if (nickname) {
		$(".nickname").text(nickname);
	}
}

function showOnOff() {
	const nickname = sessionStorage.getItem("nickname");
	if (nickname) {
		$(".on").addClass("active").siblings().removeClass("active");
	} else {
		$(".off").addClass("active").siblings().removeClass("active");
	}
}
